<template>
  <div>
    <avue-crud
      :option="option"
      :data="data"
      :permission="permissionList"
      ref="crud"
      :page.sync="page"
      :table-loading="loading"
      @on-load="onLoad"
      @row-click="getOpenDetail"
      @select="select"
      @select-all="selectAll"
      @selection-change="selectionChange"
    >
      <template slot="header">
        <div style="display: flex;align-items: center;justify-content: space-between;margin-bottom: 10px">
          <div class="left">
            {{$t("kwIntentionManage.selectedIntention")}}：{{currentRow.intentName?currentRow.intentName:''}}
          </div>

          <div class="right" style="display: flex;align-items: center;">
            <div style="margin-right: 10px;">
              <el-input
                :placeholder="$t('kwIntentionManage.intentionName')"
                v-model="intentionName"
                size="small"
                clearable
                class="kw-form-type"
              >
              </el-input>
            </div>

            <div class="kw-form-btn">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                class="kw-search-btn"
                @click="handlerSearch"
              ></el-button>
              <el-button
                size="small"
                class="kw-reset-btn"
                @click="handlerReset"
              ><i class="aicosicon aicos-icon-clear"></i
              ></el-button>
            </div>
          </div>

        </div>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
        type="text"
        size="small"
        @click.stop="handlerChoose(scope.row)"
        >
          {{ $t("aicosCommon.choose") }}
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import {intentList} from "@/api/kwKnowledgeCenter/kwIntention";

export default {

  props:{
    selectJumpRow:{
      type:Object,
      default:()=>{}
    },
  },

  mounted() {
    this.currentRow = this.selectJumpRow;
    // setTimeout(()=>{
    //   this.highlightCurrentRow('crud',this.currentRow);
    // },200)
  },

  data(){
    return{
      currentRow:{},
      intentionName:'',
      selectRow:{},
      page:{
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      loading: false,
      data: [],
      option: {
        tip: false,
        // selection: true,
        // height: 515,
        addBtn: false,
        highlightCurrentRow: true,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        searchBtn: false,
        columnBtn: false,
        refreshBtn: false,
        stripe: false,
        menu: true,
        menuAlign: "left",
        emptyText: this.$t('aicosCommon.notData'),
        column: [
          {
            label: this.$t("kwIntentionManage.intentionName"), //意图名称
            prop: "intentName",
            overHidden: true,
            html:true,
          },
          {
            label: this.$t("kwIntentionManage.intentionType"), //意图类型
            prop: "intentType",
            formatter: (val, value, label) => {
              var name
              if(val.intentType == 1){
                name = this.$t("kwIntentionManage.answerType")
              }else if (val.intentType == 2) {
                name = this.$t("kwIntentionManage.taskType")
              }else if (val.intentType == 3) {
                name = this.$t("kwIntentionManage.jumpIntention")
              }
              return name;
            },
          },
        ],
      }
    }
  },
  methods:{

    handlerChoose(row){
      this.currentRow = row;
    },

    async onLoad() {
      this.loading = true;
      this.data = [];
      const info = {
        botId: this.$store.getters.botObj.id,
        classifyId: 0,
        current: this.page.currentPage,
        size: this.page.pageSize,
        intentName: this.intentionName, //意图名称
        intentType: '', //意图类型
        trainingFlag: '', //训练状态
        quoteFilterType: '',
        intentRelevance: '',
        intentAsk: '',
      };
      const result = await intentList(info);
      if (result.data.code == 200) {
        this.data = result.data.data.records;
        this.tableDataHandle(this.data, "children");
        this.page.total = result.data.data.total;
        this.loading = false;
      }
    },

    tableDataHandle(arr, key, level = 0, indexLine = []) {
      level = level + 1;
      arr.map((item, idx) => {
        item.level = level;
        item.indexLine = Object.assign([], indexLine);
        item.indexLine.push(idx);
        item.parentLength = arr.length;
        if (item[key]) {
          this.tableDataHandle(item[key], key, level, item.indexLine);
        }
      });
      return arr;
    },

    //搜索
    handlerSearch() {
      this.page.currentPage = 1;
      this.onLoad();
      setTimeout(() => {
        this.loading = true;
        this.listIsExpand(this.data, true);
        this.loading = false;
      }, 100);
    },
    //重置
    handlerReset() {
      this.page.currentPage = 1;
      this.intentionName = "";
      this.onLoad();
      this.listIsExpand(this.data, false);
    },



  },
}
</script>

<style scoped lang="scss">
/deep/ .avue-crud__menu{
  display: none;
}

/deep/ .el-radio{
  display: contents;
}
</style>
